package com.sixmac.police.controller;

import com.sixmac.common.base.BaseController;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.kit.JsonKit;
import com.sixmac.module.AdminMenu;
import com.sixmac.module.AdminRole;
import com.sixmac.module.AdminUrole;
import com.sixmac.police.service.AdminMenuService;
import com.sixmac.police.service.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * AdminRoleController
 * 2018-09-26 11:15:35
 * @author Blade
 */
@Controller
@RequestMapping("/admin/role")
public class AdminRoleController extends BaseController {
	private static String CODE = "adminRole";
	private static String PREFIX = "t_admin_role";
	private static String LIST_SOURCE = "adminRole.list";
	private static String BASE_PATH = "/police/";
	
	@Autowired
	AdminRoleService service;
	
	@RequestMapping(KEY_MAIN)
	public String index(ModelMap mm) {
		Object grid = paginate(LIST_SOURCE);
		mm.put("grid",grid);
		return BASE_PATH + "power_list.html";
	}

	@RequestMapping(KEY_ADD)
	public String add(ModelMap mm) {
		mm.put("code", CODE);
		return BASE_PATH + "add_power.html";
	}

	@RequestMapping(KEY_EDIT + "/{id}")
	public String edit(@PathVariable Integer id, ModelMap mm) {
		AdminRole adminRole = service.findById(id);
		mm.put("model", JsonKit.toJson(adminRole));
		mm.put("id", id);
		mm.put("code", CODE);
		return BASE_PATH + "adminRole_edit.html";
	}

	@RequestMapping(KEY_VIEW + "/{id}")
	public String view(@PathVariable Integer id, ModelMap mm) {
		AdminRole adminRole = service.findById(id);
		mm.put("model", JsonKit.toJson(adminRole));
		mm.put("id", id);
		mm.put("code", CODE);
		return BASE_PATH + "adminRole_view.html";
	}

	@ResponseBody
	@RequestMapping(KEY_LIST)
	public Object list() {
		Object grid = paginate(LIST_SOURCE);
		return grid;
	}

	@ResponseBody
	@RequestMapping(KEY_SAVE)
	public AjaxResult save() {
		String items = getParameter("item");
		String role = getParameter("role");
		AdminRole adminRole = new AdminRole();
		adminRole.setName(role);
		adminRole.setCreateDate(new Date());
		int id = Blade.create(AdminRole.class).saveRtId(adminRole);

		String[] ids = items.split(",");
		for (String item:ids){
			AdminUrole adminUrole = new AdminUrole();
			adminUrole.setMenuId(Integer.valueOf(item));
			adminUrole.setRoleId(id);
			Blade.create(AdminUrole.class).save(adminUrole);
		}
		return success(SAVE_SUCCESS_MSG);
	}

	@ResponseBody
	@RequestMapping(KEY_UPDATE)
	public AjaxResult update() {
		AdminRole adminRole = mapping(PREFIX, AdminRole.class);
		boolean temp = service.update(adminRole);
		if (temp) {
			return success(UPDATE_SUCCESS_MSG);
		} else {
			return error(UPDATE_FAIL_MSG);
		}
	}

	@ResponseBody
	@RequestMapping(KEY_REMOVE)
	public AjaxResult remove(@RequestParam String ids) {
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			return success(DEL_SUCCESS_MSG);
		} else {
			return error(DEL_FAIL_MSG);
		}
	}
}
